﻿using System.Data.Entity;
using Scrubblet.FrontEnd.Models.Entities;

namespace Scrubblet.FrontEnd.Models
{
    public class EntitiesContextInitializer : DropCreateDatabaseAlways<EntitiesContext>
    {
        protected override void Seed(EntitiesContext context)
        {
            var board = new BoardConfiguration();
            context.BoardConfigurations.Add(board);
            board.Name = "Classic scrabble board";
            board.Width = board.Height = 15;
            var version = new BoardConfigurationVersion();
            board.Versions.Add(version);
            version.Version = 1;


            version.AddSpecialCell(BoardCellType.StartingAnchor, 7, 7);
            version.AddSpecialCell(BoardCellType.TrippleWord, 0, 0);
            version.AddSpecialCell(BoardCellType.TrippleWord, 7, 0);
            version.AddSpecialCell(BoardCellType.TrippleWord, 14, 0);
            version.AddSpecialCell(BoardCellType.TrippleWord, 14, 7);
            version.AddSpecialCell(BoardCellType.TrippleWord, 14, 14);
            version.AddSpecialCell(BoardCellType.TrippleWord, 7, 14);
            version.AddSpecialCell(BoardCellType.TrippleWord, 0, 14);
            version.AddSpecialCell(BoardCellType.TrippleWord, 0, 7);

            version.AddSpecialCell(BoardCellType.DoubleWord, 1, 1);
            version.AddSpecialCell(BoardCellType.DoubleWord, 2, 2);
            version.AddSpecialCell(BoardCellType.DoubleWord, 3, 3);
            version.AddSpecialCell(BoardCellType.DoubleWord, 4, 4);

            version.AddSpecialCell(BoardCellType.DoubleWord, 13, 1);
            version.AddSpecialCell(BoardCellType.DoubleWord, 12, 2);
            version.AddSpecialCell(BoardCellType.DoubleWord, 11, 3);
            version.AddSpecialCell(BoardCellType.DoubleWord, 10, 4);

            version.AddSpecialCell(BoardCellType.DoubleWord, 4, 10);
            version.AddSpecialCell(BoardCellType.DoubleWord, 3, 11);
            version.AddSpecialCell(BoardCellType.DoubleWord, 2, 12);
            version.AddSpecialCell(BoardCellType.DoubleWord, 1, 13);

            version.AddSpecialCell(BoardCellType.DoubleWord, 10, 10);
            version.AddSpecialCell(BoardCellType.DoubleWord, 11, 11);
            version.AddSpecialCell(BoardCellType.DoubleWord, 12, 12);
            version.AddSpecialCell(BoardCellType.DoubleWord, 13, 13);

            version.AddSpecialCell(BoardCellType.TrippleLetter, 1, 5);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 5, 5);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 9, 5);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 13, 5);

            version.AddSpecialCell(BoardCellType.TrippleLetter, 1, 9);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 5, 9);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 9, 9);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 13, 9);

            version.AddSpecialCell(BoardCellType.TrippleLetter, 9, 1);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 5, 1);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 9, 13);
            version.AddSpecialCell(BoardCellType.TrippleLetter, 5, 13);

            // TODO: add double-letters


            var tileSet = new TileSet();
            context.TileSets.Add(tileSet);
            tileSet.Name = "Test alphabet";
            string alphabet = "abcdefghijklmn";
            for (int i = 0; i < alphabet.Length; i++)
            {
                tileSet.Tiles.Add(new Tile{Symbol = new string(alphabet[i], 1), Value = i});
            }

            tileSet = new TileSet();
            context.TileSets.Add(tileSet);
            tileSet.Name = "English alphabet";
            tileSet.AddTiles("a", 1, 9);
            tileSet.AddTiles("b", 3, 2);
            tileSet.AddTiles("c", 3, 2);
            tileSet.AddTiles("d", 2, 4);
            tileSet.AddTiles("e", 1, 12);
            tileSet.AddTiles("f", 4, 2);
            tileSet.AddTiles("g", 2, 3);
            tileSet.AddTiles("h", 4, 2);
            tileSet.AddTiles("i", 1, 9);
            tileSet.AddTiles("j", 8, 1);
            tileSet.AddTiles("k", 5, 1);
            tileSet.AddTiles("l", 1, 4);
            tileSet.AddTiles("m", 3, 2);
            tileSet.AddTiles("n", 1, 6);
            tileSet.AddTiles("o", 1, 8);
            tileSet.AddTiles("p", 3, 2);
            tileSet.AddTiles("q", 10, 1);
            tileSet.AddTiles("r", 1, 6);
            tileSet.AddTiles("s", 1, 4);
            tileSet.AddTiles("t", 1, 6);
            tileSet.AddTiles("u", 1, 4);
            tileSet.AddTiles("v", 4, 2);
            tileSet.AddTiles("w", 4, 2);
            tileSet.AddTiles("x", 8, 1);
            tileSet.AddTiles("y", 4, 2);
            tileSet.AddTiles("z", 10, 1);
            tileSet.AddTiles(" ", 0, 2);

            context.SaveChanges();
        }
    }
}